package ${basePackage}.generator;
import ${basePackage}.model.DataModel;
import freemarker.template.TemplateException;
import cn.hutool.core.io.FileUtil;
import java.io.File;
import java.io.IOException;


<#macro  generateFile  fileInfo >
    inputPath = new File(inputRootPath,"${fileInfo.inputPath}").getAbsolutePath();
    outputPath = new File(outputRootPath,"${fileInfo.outputPath}").getAbsolutePath();
    if(!FileUtil.exist(inputPath)){
    FileUtil.touch(inputPath);
    }
    if(!FileUtil.exist(outputPath)){
    FileUtil.touch(outputPath);
    }
    <#if fileInfo.generateType == "static">
        StaticGenerator.copyFilesByHutool(inputPath,outputPath);
    <#else>
        DynamicGenerator.doGenerate(inputPath,outputPath,model);
    </#if>
</#macro>

/**
* 核心生成器
*/
public class FileGenerator {

/**
* 生成
*
* @param model 数据模型
* @throws TemplateException
* @throws IOException
*/
public static void doGenerate(DataModel model) throws TemplateException, IOException {

    String inputRootPath = "${fileConfig.inputRootPath}";
    String outputRootPath = "${fileConfig.outputRootPath}";

    String inputPath;
    String outputPath;

<#--        获取模型变量-->
<#list  modelConfig.models as modelInfo >
<#if modelInfo.groupKey??>
    <#list modelInfo.models as subModelInfo>
        <#if subModelInfo.fieldName?? >
        ${subModelInfo.type} ${subModelInfo.fieldName} = model.${modelInfo.groupKey}.${subModelInfo.fieldName};
        </#if>
    </#list>
<#else>
    <#if modelInfo.fieldName?? >
         ${modelInfo.type} ${modelInfo.fieldName} = model.${modelInfo.fieldName};
    </#if>
</#if>
</#list>
/*------------------------------*/
    <#--开去文件信息-->
    <#list fileConfig.files as fileInfo>
        <#if fileInfo.groupKey??>
            // groupKey = ${fileInfo.groupKey}
            <#if fileInfo.condition??>
                if(${fileInfo.condition}){
                <#list fileInfo.files as fileInfo>
                    /*------------------------------*/
                    <@generateFile fileInfo=fileInfo/>
                </#list>
                }
            <#else>
                <#list fileInfo.files as fileInfo>
                    /*------------------------------*/
                    <@generateFile fileInfo=fileInfo/>
                </#list>
            </#if>
        <#else>
            <#if fileInfo.condition??>
                if(${fileInfo.condition}){
                <@generateFile fileInfo=fileInfo/>
                }
            <#else>
                <@generateFile fileInfo=fileInfo/>
            </#if>
        </#if>
        /*------------------------------*/
    </#list>

}

}


